class CheckInGoal {
  final int? id;
  final String title; // 目标标题
  final String description; // 目标描述
  final int targetValue; // 目标值
  final String unit; // 单位（如分钟、个数等）
  final bool isActive; // 是否激活
  final int createdAt;
  final int updatedAt;

  CheckInGoal({
    this.id,
    required this.title,
    this.description = '',
    required this.targetValue,
    required this.unit,
    this.isActive = true,
    required this.createdAt,
    required this.updatedAt,
  });

  // 从Map创建CheckInGoal对象
  factory CheckInGoal.fromMap(Map<String, dynamic> map) {
    return CheckInGoal(
      id: map['id']?.toInt(),
      title: map['title'] ?? '',
      description: map['description'] ?? '',
      targetValue: map['target_value']?.toInt() ?? 0,
      unit: map['unit'] ?? '',
      isActive: map['is_active'] == 1,
      createdAt: map['created_at']?.toInt() ?? DateTime.now().millisecondsSinceEpoch,
      updatedAt: map['updated_at']?.toInt() ?? DateTime.now().millisecondsSinceEpoch,
    );
  }

  // 转换为Map
  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'title': title,
      'description': description,
      'target_value': targetValue,
      'unit': unit,
      'is_active': isActive ? 1 : 0,
      'created_at': createdAt,
      'updated_at': updatedAt,
    };
  }

  // 复制并修改
  CheckInGoal copyWith({
    int? id,
    String? title,
    String? description,
    int? targetValue,
    String? unit,
    bool? isActive,
    int? createdAt,
    int? updatedAt,
  }) {
    return CheckInGoal(
      id: id ?? this.id,
      title: title ?? this.title,
      description: description ?? this.description,
      targetValue: targetValue ?? this.targetValue,
      unit: unit ?? this.unit,
      isActive: isActive ?? this.isActive,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
    );
  }

  @override
  String toString() {
    return 'CheckInGoal{id: $id, title: $title, targetValue: $targetValue, unit: $unit}';
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is CheckInGoal && other.id == id;
  }

  @override
  int get hashCode => id.hashCode;
}